-module(main).

-export([
    start/0
    ,stop/0
    ,stop_and_backup/0

    ,stop_node/1
]).

-include("common.hrl").
-include("env.hrl").


start() ->
    application:ensure_all_started(app).

stop() ->
    [Node2] = init:get_plain_arguments(),
    Node	= list_to_atom(Node2),
    ?INFO("服务器准备关闭:~w", [Node]),
    Reply = rpc:call(Node, main, stop_node, [normal]),
    ?INFO("服务器关闭返回:~w", [Reply]),
    erlang:halt().

stop_and_backup() ->
    [Node2] = init:get_plain_arguments(),
    Node	= list_to_atom(Node2),
    ?INFO("服务器准备关闭:~w", [Node]),
    Reply = rpc:call(Node, main, stop_node, [backup]),
    ?INFO("服务器关闭返回:~w", [Reply]),
    erlang:halt().


stop_node(Type) ->
    net_sup2:stop(),
    role_online_mgr:stop_all_role(),

    application:stop(game),

    case Type of
        backup ->
            DirVar = env_mgr:get(?ENV_DIR_VAR),
            Path = lists:concat([DirVar, "mnesia_backup.mb"]),
            db_mnesia:backup(Path);
        _ -> ok
    end,

    application:stop(mnesia),
    erlang:halt().
